package com.difsonapps.android.marker.beans;

import org.json.JSONException;
import org.json.JSONObject;

import com.difsonapps.android.marker.AppConstant;
import com.difsonapps.android.marker.util.MarkerUtil;

import android.os.Parcel;
import android.os.Parcelable;

public class Video  implements Parcelable{
	private int id, countMarks=0, countLikes=0, originalId=0;
	private double avgMarks = 0;
	
	private boolean iLike = false;
    private String name, description, urlThumbnail, urlVideo, timestamp, createdAt, modifiedAt;
	
    public Video() {
		super();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	

	public int getCountMarks() {
		return countMarks;
	}

	public void setCountMarks(int countMarks) {
		this.countMarks = countMarks;
	}

	public int getCountLikes() {
		return countLikes;
	}

	public void setCountLikes(int countLikes) {
		this.countLikes = countLikes;
	}

	public int getOriginalId() {
		return originalId;
	}

	public void setOriginalId(int originalId) {
		this.originalId = originalId;
	}

	public double getAvgMarks() {
		return avgMarks;
	}

	public void setAvgMarks(double avgMarks) {
		this.avgMarks = avgMarks;
	}

	public boolean isiLike() {
		return iLike;
	}

	public void setiLike(boolean iLike) {
		this.iLike = iLike;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getUrlThumbnail() {
		return urlThumbnail;
	}

	public void setUrlThumbnail(String urlThumbnail) {
		this.urlThumbnail = urlThumbnail;
	}

	public String getUrlVideo() {
		return urlVideo;
	}

	public void setUrlVideo(String urlVideo) {
		this.urlVideo = urlVideo;
	}

	public String getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(String timestamp) {
		this.timestamp = timestamp;
	}

	public String getCreatedAt() {
		return createdAt;
	}

	public void setCreatedAt(String createdAt) {
		this.createdAt = createdAt;
	}

	public String getModifiedAt() {
		return modifiedAt;
	}

	public void setModifiedAt(String modifiedAt) {
		this.modifiedAt = modifiedAt;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Video other = (Video) obj;
		if (id != other.id)
			return false;
		return true;
	}

	public Video(Parcel in){
		this();
		this.id = in.readInt();
		this.countMarks = in.readInt();
		this.avgMarks = in.readDouble();
		this.countLikes = in.readInt();
		this.iLike = in.readInt()==1;
		this.name = in.readString();
		this.description = in.readString();
		this.urlThumbnail = in.readString();
		this.urlVideo = in.readString();
		this.timestamp = in.readString();
		this.createdAt = in.readString();
		this.modifiedAt = in.readString();
		this.originalId = in.readInt();
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(id);
		dest.writeInt(countMarks);
		dest.writeDouble(avgMarks);
		dest.writeInt(countLikes);
		dest.writeInt(iLike?1:0);
		dest.writeString(name);
		dest.writeString(description);
		dest.writeString(urlThumbnail);
		dest.writeString(urlVideo);
		dest.writeString(timestamp);
		dest.writeString(createdAt);
		dest.writeString(modifiedAt);
		dest.writeInt(originalId);
	}	
	
	public static final Parcelable.Creator<Video> CREATOR = new
		Parcelable.Creator<Video>() {
			@Override
			public Video createFromParcel(Parcel source) {
				return new Video(source);
			}
			@Override
			public Video[] newArray(int size) {
				return new Video[size];
		}
	};

	@Override
	public int describeContents() {
		return 0;
	}
	
	public static Video parseFromJsonObject(JSONObject json){
		if(json==null || !json.has("id")) return null;
		Video result = new Video();
		try {
			result.setId(json.getInt("id"));
			result.setCountMarks(json.getInt("count_marks"));
			result.setAvgMarks(json.has("avg_marks")?json.getDouble("avg_marks"):0);
			result.setCountLikes(json.has("count_likes")?json.getInt("count_likes"):0);
			result.setiLike(json.has("i_like")?json.getBoolean("i_like"):false);
			result.setOriginalId(json.has("original_id")?json.getInt("original_id"):0);
			result.setName(json.getString("name"));
			String url_thumbnail = json.getString("url_thumbnail");
			result.setUrlThumbnail(MarkerUtil.buildImageUrl(url_thumbnail, AppConstant.IMAGES_FOLDER, null));
			String url_video = json.getString("url_video");
			result.setUrlVideo(MarkerUtil.buildImageUrl(url_video, AppConstant.VIDEOS_FOLDER, null));
			
			result.setDescription(json.getString("description"));
			result.setTimestamp(json.getString("timestamp"));
			result.setCreatedAt(json.getString("created_at"));
			result.setModifiedAt(json.getString("modified_at"));
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		return result;
	}
    

}
